import java.awt.event.*;
import java.awt.*;
import javax.swing.*;;
import java.util.*;

class UserEditor extends JDialog implements ActionListener
{

	int mship;
    String password;
    String mname;
    String username;
    int isadmin;
    String address;
    String sname;
    ManageUser owner;

	JTextField mshipTF;
	JTextField passwordTF;
	JTextField mnameTF;
	JTextField usernameTF;
	JTextField isadminTF;
	JTextField addressTF;
	JTextField snameTF;

	JButton commit;

    String DBusername = "db2admin"; // Need you database Username and password
    String DBpassword = "crossbow";
    String database = "RENTMOV";

	UserEditor(int mship, ManageUser owner){

		super();
		this.mship = mship;
		this.owner = owner;
		if(mship > -1)
			getUserInfo();

		Dimension textFieldDim = new Dimension(150,20);

		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel mshipP = new JPanel(new FlowLayout());
        mshipTF = new JTextField();
        mshipTF.setEditable(false);
        mshipTF.setMaximumSize(textFieldDim);
        mshipTF.setPreferredSize(textFieldDim);
        mshipTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel mshipLB = new JLabel("Mship#");
        mshipP.add(mshipLB);
        mshipP.add(mshipTF);
        mainPanel.add(mshipP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel passP = new JPanel(new FlowLayout());
        passwordTF = new JTextField();

        passwordTF.setMaximumSize(textFieldDim);
        passwordTF.setPreferredSize(textFieldDim);
        passwordTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel passLB = new JLabel("Password");
        passP.add(passLB);
        passP.add(passwordTF);
        mainPanel.add(passP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel mnameP = new JPanel(new FlowLayout());
        mnameTF = new JTextField();
        mnameTF.setMaximumSize(textFieldDim);
        mnameTF.setPreferredSize(textFieldDim);
        mnameTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel mnameLB = new JLabel("MName");
        mnameP.add(mnameLB);
        mnameP.add(mnameTF);
        mainPanel.add(mnameP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel usernameP = new JPanel(new FlowLayout());
        usernameTF = new JTextField();
        usernameTF.setMaximumSize(textFieldDim);
        usernameTF.setPreferredSize(textFieldDim);
        usernameTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel usernameLB = new JLabel("Username");
        usernameP.add(usernameLB);
        usernameP.add(usernameTF);
        mainPanel.add(usernameP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel isadminP = new JPanel(new FlowLayout());
        isadminTF = new JTextField();
        isadminTF.setMaximumSize(textFieldDim);
        isadminTF.setPreferredSize(textFieldDim);
        isadminTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel isadminLB = new JLabel("Is Admin");
        isadminP.add(isadminLB);
        isadminP.add(isadminTF);
        mainPanel.add(isadminP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel addressP = new JPanel(new FlowLayout());
        addressTF = new JTextField();
        addressTF.setMaximumSize(textFieldDim);
        addressTF.setPreferredSize(textFieldDim);
        addressTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel addressLB = new JLabel("Address");
        addressP.add(addressLB);
        addressP.add(addressTF);
        mainPanel.add(addressP);

        mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
        JPanel snameP = new JPanel(new FlowLayout());
        snameTF = new JTextField();
        snameTF.setMaximumSize(textFieldDim);
        snameTF.setPreferredSize(textFieldDim);
        snameTF.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel snameLB = new JLabel("Sname");
        snameP.add(snameLB);
        snameP.add(snameTF);
        mainPanel.add(snameP);

		mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
		if(mship > -1){
			commit = new JButton("Commit");
		}else{
			commit = new JButton("Add");
		}
		commit.setMaximumSize(new Dimension(100,20));
		commit.setPreferredSize(new Dimension(100,20));
		commit.addActionListener(this);
		if(mship > -1){
			commit.setActionCommand("commit");
		}else{
			commit.setActionCommand("add");
		}
		mainPanel.add(commit);

		if(mship > -1){
			mshipTF.setText(Integer.toString(mship));
			passwordTF.setText(password);
			mnameTF.setText(mname);
			usernameTF.setText(username);
			isadminTF.setText(Integer.toString(isadmin));
			addressTF.setText(address);
			snameTF.setText(sname);
		}


        Container c = getContentPane();
        c.add(mainPanel);

        Toolkit tk;
        Dimension d;
        tk = Toolkit.getDefaultToolkit();
        d = tk.getScreenSize();
        setSize(250,400);
        setLocation((d.width/2)-125, (d.height/2)-200);
        setVisible(true);

	}

	public void actionPerformed(ActionEvent e){

		if(e.getActionCommand().equals("commit")){

			String statement;
			statement = "UPDATE MEMBERS " +
			"set password=\'" + passwordTF.getText() + "\', " +
			"mname=\'" + mnameTF.getText() + "\', " +
			"username=\'" + usernameTF.getText() + "\', " +
			"isadmin=" + isadminTF.getText() + ", " +
			"address=\'" + addressTF.getText() + "\', " +
			"sname=\'" + snameTF.getText() + "\' " +
			"where mship#=" + mship;

			try{
				DBQTable dbq = new DBQTable(database);
				int updateCount = dbq.update(DBusername,DBpassword,statement);
				owner.reloadList();
				JOptionPane.showMessageDialog(null,
				    "Update was successful",
				    "Update Processed",
    				JOptionPane.PLAIN_MESSAGE);
			}catch (Exception x){
				JOptionPane.showMessageDialog(null,
				    "Update was unsuccessful",
				    "Update Failed",
    				JOptionPane.ERROR_MESSAGE);
    			x.printStackTrace();
			}

		}else if(e.getActionCommand().equals("add")){

			DBQTable dbq = new DBQTable(database);
			Vector mshipV;

			try{

				mshipV = dbq.getVector(DBusername,DBpassword,"Select max(M.MSHIP#) from MEMBERS M");
				mship = (Integer) mshipV.elementAt(0);
				mship++;
				System.out.println("New Mship#: " + mship);
				String statement;
				statement = "INSERT INTO MEMBERS " +
				"VALUES (" + mship + ", \'" +
				passwordTF.getText().trim() + "\', \'" +
				mnameTF.getText().trim() + "\', \'" +
				usernameTF.getText().trim() + "\', " +
				isadminTF.getText().trim() + ", \'" +
				addressTF.getText().trim() + "\', \'" +
				snameTF.getText().trim() + "\')";

				int updateCount = dbq.update(DBusername,DBpassword,statement);
				owner.reloadList();
				JOptionPane.showMessageDialog(null,
				    "Insert was successful\nmship#: " + mship,
				    "Insert Processed",
    				JOptionPane.PLAIN_MESSAGE);

			}catch (Exception x){
				JOptionPane.showMessageDialog(null,
				    "Insert was unsuccessful",
				    "Insert Failed",
    				JOptionPane.ERROR_MESSAGE);
			}

		}


	}

	private void getUserInfo(){


		DBQTable dbq = new DBQTable(database);
		try{

			JTable table = dbq.getTable(DBusername,DBpassword,"Select * from MEMBERS M where mship#=" + mship);
			password = (String) table.getModel().getValueAt(0,1);
			mname = (String) table.getModel().getValueAt(0,2);
			username = (String) table.getModel().getValueAt(0,3);
			isadmin = (Integer) table.getModel().getValueAt(0,4);
			address = (String) table.getModel().getValueAt(0,5);
			sname = (String) table.getModel().getValueAt(0,6);


		}catch (Exception x){
			x.printStackTrace();
		}
	}

}